import type { SysMenu } from "@/entity/SysMenu";
import { BaseEntityService } from "./base-entity.service";
import httpClient from "./http-client";
import type { AxiosResponse } from "axios";
import type { ResponseResult } from "@/entity/ResponseResult";
import type { AssignMenuRoleParam } from "@/entity/AssignMenuRoleParam";

export class SysMenuService extends BaseEntityService<SysMenu> {
    baseUrl = "/api/v1.0/admin/menu";

    async treeList() {
        return httpClient.post(`${this.baseUrl}/tree-list`).then((res: AxiosResponse<ResponseResult<SysMenu[]>>) => {
            if (res.data.data) {
                const tree = res.data.data;
                return Promise.resolve(tree);
            } else {
                throw "no data";
            }
        });
    }

    async findMenusByRoleId(roleId: number) {
        return httpClient.get(`${this.baseUrl}/${roleId}/menus`).then((res: AxiosResponse<ResponseResult<SysMenu[]>>) => {
            if (res.data.data) {
                const tree = res.data.data;
                return Promise.resolve(tree);
            } else {
                throw "no data";
            }
        });
    }

    async assign(param: AssignMenuRoleParam) {
        const body = {
            ...param,
        }
        return httpClient.post(`${this.baseUrl}/assign`, body);
    }

    async cancelAssign(param: AssignMenuRoleParam) {
        const body = {
            ...param,
        }
        return httpClient.post(`${this.baseUrl}/cancel-assign`, body);
    }
}

const menuService = new SysMenuService();

export default menuService;